GBML manual / Events

destroy

Runs once, when this instance is destroyed - immediately, not at the end of the frame (S2.6).

Syntax

destroy: fires once, the instant instance_destroy() is called on this instance

Description

Runs once for each instance that was destroyed with instance_destroy, at the end of the frame it was destroyed in. The instance finishes that frame first: the rest of the event that destroyed it, its Step End, its Draw and everything else the frame still had for it run as usual. Only after the frame has been shown does Destroy run, and then the instance is removed. So a line written right after instance_destroy(self); does not see anything Destroy changed, because Destroy has not run yet.

Destroy runs exactly once, however many times the instance was destroyed, and from wherever: by itself, by another instance, from a with, from a script, or from its own Destroy event.

Anything Destroy does (spawning a death effect, awarding a score) happens at that moment, after everything else the frame did. The instance is still whole while Destroy runs: its x, y and variables are all there. Right after Destroy, the Cleanup event runs and the instance is gone. Code that must run whenever the instance is gone, however it went, belongs in Cleanup instead.

Two things to know:

A room change does not run Destroy for the instances it removes: leaving a room is not the instance being destroyed, so they get only Cleanup. An instance that was destroyed before the room ended still gets its Destroy event, just before the room is cleared.

Example

instance_create(x, y, obj_doc_pickup);

Leaves a pickup behind at this instance's own position, at the end of the frame it is destroyed in.

See also: cleanupcreateEventsinstance_destroy